package com.uniriho.lxr0860_printer;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import androidx.core.content.FileProvider;

import java.io.File;

public class PrinterShareMgr {
    private static PrinterShareMgr mgr = new PrinterShareMgr();

    public static PrinterShareMgr getInstance() {
        return mgr;
    }

    private PrinterShareMgr() {
    }

    public void printFile(Context context, String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            data = FileProvider.getUriForFile(context, "com.uniriho.lxr0860_printer.provider", new File(filePath));
        } else {
            data = Uri.fromFile(new File(filePath));
        }
        Log.i("zsm", "uri:" + data);
        ComponentName comp = null;
        if (filePath.endsWith("pdf")) {
            intent.setDataAndType(data,"application/pdf");
            comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintDocuments");
        } else if (filePath.endsWith("doc") || filePath.endsWith("docx")||filePath.endsWith("txt")){
            intent.setDataAndType(data,"application/doc");
            comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintDocuments");
        }else if (filePath.endsWith("jpg") || filePath.endsWith("jpeg") || filePath.endsWith("gif") || filePath.endsWith("png")){
            intent.setDataAndType(data,"image/jpeg");
            comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityPrintPictures");
        }else if (filePath.endsWith("html")||filePath.endsWith("htm")){
            intent.setDataAndType(data,"text/html");
            comp = new ComponentName("com.dynamixsoftware.printershare", "com.dynamixsoftware.printershare.ActivityWeb");
        }
        intent.setComponent(comp);
        context.startActivity(intent);
    }
}
